using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using KingSite.Shop.Core.Service;
using KingSite.Shop.Service;
using KingSite.Shop.Domain.ShowDataModel;
using KingSite.Shop.Core.Mvc;

namespace KingSite.Shop.Web.Controllers
{
    public class CartController : BaseController
    {
        //
        // GET: /ShoppingCart/
        
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Add(int id) {
            IShoppingCart cart = new ShoppingCart(1, 1);
            int quantity = 1;
            if (!string.IsNullOrEmpty(Request["quantity"])) {
                quantity = int.Parse(Request["quantity"]);
            }
            cart.Add(id, quantity);
            return RedirectToAction("Index");
        }

        #region UserControl

        public ActionResult ShoppingCart() {
            ShowCart();
            return View();
        }

        private void ShowCart() {
            IShoppingCart cart = new ShoppingCart(1, 1);
            ViewData.Model = cart;
        }

        public ActionResult MiniShoppingCart() {
            ShowCart();
            return View();
        }
        #endregion

        #region AjaxService

        public ActionResult AjaxService() { 
            string action = Request["action"];
            if (!string.IsNullOrEmpty(action) ) {
                switch (action.ToLower()) {
                    case "add": { Add(); break; }
                    case "delete": { Remove(); break; }
                    case "change": { Change(); break; }
                }
            }
            return RedirectToAction("ShoppingCart");
        }

        [NonAction]
        private void Add() {
            IShoppingCart cart = new ShoppingCart(1, 1);
            int skuId = 0;
            int.TryParse(Request["skuId"], out skuId);
            int quantity = 1;
            int.TryParse(Request["quantity"], out quantity);
            cart.Add(skuId, quantity);
        }
        [NonAction]
        private void Remove() {
            IShoppingCart cart = new ShoppingCart(1, 1);
            int id = 0;
            int.TryParse(Request["id"], out id);
            cart.Remove(id);

        }
        [NonAction]
        private void Change() {
            IShoppingCart cart = new ShoppingCart(1, 1);
            int id = 0;
            int.TryParse(Request["id"], out id);
            int quantity = 1;
            int.TryParse(Request["quantity"], out quantity);
            cart.SetQuantity(id, quantity);
        }

        #endregion

    }
}
